//假设的帖子数据
const posts = [
    { id: 1, content: '寻找一个温暖的家', comments: [], likes: 0 },
    // 更多帖子...
];

// 显示帖子列表
function displayPosts() {
    const postsContainer = document.getElementById('posts');
    postsContainer.innerHTML = ''; // 清空帖子列表
    posts.forEach(post => {
        const postElement = document.createElement('div');
        postElement.className = 'post';
        postElement.innerHTML = `
            <div class="post-header">
                <h3>${post.content}</h3>
                <div class="post-actions">
                    <button onclick="likePost(${post.id})">点赞</button>
                    <button onclick="deletePost(${post.id})">删除</button>
                </div>
            </div>
            <div>
                ${post.comments.map(comment => `<p>${comment}</p>`).join('')}
                <textarea placeholder="添加评论..."></textarea>
                <button onclick="addComment(${post.id})">评论</button>
            </div>
        `;
        postsContainer.appendChild(postElement);
    });
}

// 搜索帖子
function searchPosts() {
    const searchQuery = document.getElementById('searchInput').value.toLowerCase();
    // 这里应该调用后端API进行搜索，现在只是简单过滤
    const filteredPosts = posts.filter(post => post.content.toLowerCase().includes(searchQuery));
    displayPosts(filteredPosts);
}

// 发布帖子
function publishPost() {
    const newPostContent = document.getElementById('postContent').value;
    if (newPostContent.trim() === '') return;
    const newPost = { id: posts.length + 1, content: newPostContent, comments: [], likes: 0 };
    posts.push(newPost);
    displayPosts();
    document.getElementById('postContent').value = ''; // 清空输入框
}

// 点赞帖子
function likePost(postId) {
    const post = posts.find(p => p.id === postId);
    if (post) {
        post.likes++;
        displayPosts();
    }
}

// 删除帖子
function deletePost(postId) {
    const index = posts.findIndex(p => p.id === postId);
    if (index > -1) {
        posts.splice(index, 1);
        displayPosts();
    }
}

// 添加评论
function addComment(postId) {
    const commentText = `评论内容${new Date().toLocaleTimeString()}`; // 这里应该从输入框获取评论内容
    const post = posts.find(p => p.id === postId);
    if (post) {
        post.comments.push(commentText);
        displayPosts();
    }
}

// 初始化显示帖子列表
displayPosts();